<?php
class Streamconfig
{
    protected $CI;
    public $errorMessage = array();
    
    public function __construct()
    {
        $this->CI = & get_instance();
    }
    
    /**
     * 设置错误信息
     * @param unknown $message
     */
    public function setError($message)
    {
        $this->errorMessage[] = $message;
    }
    
    /**
     * 获取错误信息
     * @return multitype:
     */
    public function getError()
    {
        return $this->errorMessage;
    }

    /**
     * 所有上级用户的uid
     * @param $uid
     * @return array
     */
    function getTargets($uid)
    {
        $i = 0;
        $targets = [];
        //限制16级查询防止死循环
        while ($uid != UTID_BEIZHU && $uid != 0 && $i < 16) {
            $pid = 0;
            $utid = 0;
            //常规查询 当前UID
            $query_user = $this->user->findOne(['uid'=>$uid], 'uid, user_type, parent_id');
            //没有user信息
            if ($query_user->num_rows() <= 0) {
                $uid = 0;
                $utid = 0;
                break;
            }
            $row_user = $query_user->row();
            if ($row_user->parent_id == 0) {
                $uid = 0;
                $utid = 0;
                break;
            } else {
                $utid = $row_user->user_type;
                $pid = $row_user->parent_id;
            }
            //array_push($targets, array('uid'=>$uid, 'user_type'=>$utid));
            $targets[] = json_decode(json_encode(['uid'=>$uid, 'user_type'=>$utid])); //速度快于array_push
            $uid = $pid;
            $i++;
        }
        return $targets;
    }

    /**
     * 得到符合条件的第一个分配 用户。
     * @param $user_type
     * @param $targets
     * @return int
     */
    public function firstTarget($user_type, $targets)
    {
        //插座user_type
        $slot_utid = intval($user_type);
    
        foreach ($targets as $target) {
            //如果有符合条件的操作对象
            if ($slot_utid & intval($target->user_type)) {
                //echo $slot_utid."@".intval($target['utid']);exit();
                return $target->uid;
            }
        }
        //如果没有指定该项分配对象 则指定默认的着想账号为分配对象
        return $this->getDefaultUID($slot_utid);
    }
    
    public function getDefaultUID($utid)
    {
        switch ($utid) {
            case UTID_PROVIDER:
                return UTID_PROVIDER;
            case UTID_PURCHASER:
                return UTID_PURCHASER;
            case UTID_BEIZHU:
                return UTID_BEIZHU;
            case UTID_MANAGER_L1:
                return UTID_MANAGER_L1;
            case UTID_MANAGER_L2:
                return UTID_MANAGER_L2;
            case UTID_GROWTH:
                return UTID_GROWTH;
            case UTID_SELLER:
                return UTID_SELLER;
            case UTID_CUSTOMER:
                return UTID_CUSTOMER;
            case UTID_TELLER:
                return UTID_TELLER;
            default:
                return 0;
        }
    }

    /**
     * 获取当前用户类型
     * @param unknown $uid
     * @return multitype:string
     */
    public function getUserType($uid)
    {
        $result = $this->user->findByIds(array('uid'=>$uid));
        if ($result->num_rows() <= 0) {
            return 0;
        }
        $userInfo = $result->row(0);
    
        $bitusers = array(UTID_PROVIDER, UTID_PURCHASER, UTID_BEIZHU, UTID_MANAGER_L1, UTID_MANAGER_L2, UTID_GROWTH, UTID_SELLER);
        foreach ($bitusers as $type) {
            if ((intval($type)& intval($userInfo->user_type)) != intval($type)) {
                continue;
            }
            return $type;
        }
        return 0;
    }
}





